<HTML><title>Function Objects</title>
<BODY>
<h2><a name="Selection"></a>Example 3: Selection views based on conditions </h2>
<p>Using condition functions (predicates), we can filter away uninteresting data 
  and keep only interesting data. In physics codes, this process is often called 
  <i>cutting on a predicate</i>. 
<hr>
<h2>Conditions on 1-d matrices (vectors) </h2>
<pre>
// the naming shortcut (alias) saves some keystrokes:
cern.jet.math.Functions F = cern.jet.math.Functions.functions;

double[] v1 = {0, 1, 2, 3};
DoubleMatrix1D matrix = new DenseDoubleMatrix1D(v1);
// 0 1 2 3 

// view all cells for which holds: lower <= value <= upper
final double lower = 0.2;
final double upper = 2.5
matrix.viewSelection(F.isBetween(lower, upper)); 
// --> 1 2

// equivalent, but less concise:
matrix.viewSelection( 
	new DoubleProcedure() {
		public final boolean apply(double a) { return lower &lt;= a &amp;&amp; a &lt;= upper; }
	}
);
// --> 1 2
</pre>
<pre></pre>
<pre>// view all cells with even value
matrix.viewSelection( 
	new DoubleProcedure() {
		public final boolean apply(double a) { return a % 2 == 0; }
	}
);
// --> 0 2

// sum of all cells for which holds: lower <= value <= upper
double sum = matrix.viewSelection(F.isBetween(lower, upper)).zSum(); 
// --> 3
<br>// equivalent: 
double sum = matrix.viewSelection(F.isBetween(lower, upper)).aggregate(F.plus,F.identity); 
</pre>
<hr>
<h2>Conditions on 2-d matrices </h2>
<pre>
// view all rows which have a value < threshold in the first column (representing "age")
final double threshold = 16;
matrix.viewSelection( 
	new DoubleMatrix1DProcedure() {
		public final boolean apply(DoubleMatrix1D m) { return m.get(0) < threshold; }
	}
);

// view all rows with RMS < threshold.<br>// the RMS (Root-Mean-Square) is a measure of the average "size" of the elements of a data sequence.
final double threshold = 0.5;
matrix.viewSelection( 
	new DoubleMatrix1DProcedure() {
		public final boolean apply(DoubleMatrix1D m) { return Math.sqrt(m.aggregate(F.plus,F.square) / m.size()) < threshold; }
	}
);
</pre>
<hr>
<h2>Conditions on 3-d matrices</h2>
<pre>
// view all slices which have an aggregate sum > 1000
matrix.viewSelection( 
	new DoubleMatrix2DProcedure() {
		public final boolean apply(DoubleMatrix2D m) { return m.zSum() > 1000; }
	}
);
</pre>
</BODY>
</HTML>